<template>
	<!-- 子表信息折叠面板 -->
	<view class="collapse" :style="{borderRadius: borderRadius + 'px'}">
		<view :class="['collapse_title', hasIcon ? 'has-icon' : '']" @click="isOpen = !isOpen">
			<view class="collapse_name">{{ name }}</view>
			<image src="@/static/synergy/arrow.png" mode="aspectFill"
				:style="{transform: isOpen?'rotate(0deg)':'rotate(180deg)'}"></image>
		</view>
		<view :style="{ height: isOpen?'2rpx':'0' }" class="line"></view>
		<view class="collapse_action" :id="elId" :style="{ height: isOpen?mainHeight + 'px':0 }">
			<view :class="['listsmall', hasIcon ? 'half-width' : '']" v-if="dataSource.length > 0">
				<view class="action" v-for="(item,key) in childFields" :key="item">
					<view class="list1">{{ key }}</view>
					<view class="list2">{{ dataSource[0][item]}}</view>
				</view>
			</view>
			<u-empty v-else mode="data" icon="/static/empty/data.png" :text="emptyText"
				:textColor="emptyTextColor"></u-empty>
			<block v-if="dataSource.length > 1">
				<view class="child_action" :id="childId" :style="{ height: actionOpen?actionHeight + 'px':0 }">
					<block v-for="value in dataSource.slice(1)" :key="value.id">
						<view class="line" :style="{ height: actionOpen?'2rpx':'0' }"></view>
						<view :class="['listsmall', hasIcon ? 'half-width' : '']">
							<view class="action" v-for="(item,key) in childFields" :key="item">
								<view class="list1">{{ key }}</view>
								<view class="list2">{{ value[item] }}</view>
							</view>
						</view>
					</block>
				</view>
				<!-- <view class="button" @click="openStatus">{{ actionOpen?'收起':'展开' }}</view> -->
			</block>
		</view>
		<slot name="handler"></slot>
	</view>
</template>
<script>
	// import detailInformationList from './detailInformationList.vue'
	export default {
		data() {
			return {
				isOpen: true,
				actionHeight: '',
				actionOpen: true,
				mainHeight: null,
				elId: uni.$u.guid(36),
				childId: uni.$u.guid(),
			}
		},
		components: {
			// detailInformationList
		},
		props: {
			name: {
				type: String
			},
			dataSource: {
				type: Array,
			},
			childFields: {
				type: Object,
				require: true
			},
			borderRadius: {
				default: 10
			},
			hasIcon: {
				default: false
			},
			emptyText: {
				type: String
			},
			emptyTextColor: {
				type: String
			}
		},
		watch: {
			dataSource: {
				handler(val) {
					this.$nextTick(function() {
						this.setContentMain()
						if (val.length > 1) {
							this.setContentChild()
						}
					})
				}
			},
			emptyText: {
				handler(val) {
					if (val) {
						this.$nextTick(function() {
							this.setContentMain()
						})
					}
				},
				immediate: true
			},
		},
		methods: {
			async setContentMain() {
				// 每次面板打开或者收起时，都查询元素尺寸
				// 好处是，父组件从服务端获取内容后，变更折叠面板后可以获得最新的高度
				const rect = await this.queryRect()
				// this.mainHeight = rect.height
				this.timer = setTimeout(() => {
					clearTimeout(this.timer)
					this.timer = null
					this.mainHeight = rect.height
				}, 300)
			},
			async setContentChild() {
				// 每次面板打开或者收起时，都查询元素尺寸
				// 好处是，父组件从服务端获取内容后，变更折叠面板后可以获得最新的高度
				const rect = await this.queryChildRect()
				this.actionHeight = rect.height
			},
			openStatus() {
				if (this.actionOpen) {
					this.mainHeight = this.mainHeight - this.actionHeight
				} else {
					this.mainHeight = this.mainHeight + this.actionHeight
				}
				this.actionOpen = !this.actionOpen
			},
			// 查询内容高度
			queryRect() {
				// #ifndef APP-NVUE
				// $uGetRect为uView自带的节点查询简化方法，详见文档介绍：https://www.uviewui.com/js/getRect.html
				// 组件内部一般用this.$uGetRect，对外的为uni.$u.getRect，二者功能一致，名称不同
				return new Promise(resolve => {
					this.$uGetRect(`#${this.elId}`).then(size => {
						resolve(size)
					})
				})
				// #endif
			},
			// 查询子节点内容高度
			queryChildRect() {
				// #ifndef APP-NVUE
				// $uGetRect为uView自带的节点查询简化方法，详见文档介绍：https://www.uviewui.com/js/getRect.html
				// 组件内部一般用this.$uGetRect，对外的为uni.$u.getRect，二者功能一致，名称不同
				return new Promise(resolve => {
					this.$uGetRect(`#${this.childId}`).then(size => {
						resolve(size)
					})
				})
				// #endif
			}
		}
	}
</script>
<style lang="scss" scoped>
	@import "@/pages/synergy/common/actionNew.scss";

	.child_action {
		transition: height .4s ease-in-out, -webkit-transform .4s ease-in-out, transform .4s ease-in-out;
		transform-origin: 50% 50% 0px;
		overflow: hidden;

		.line {
			margin: 0 auto;
			width: 92%;
		}
	}

	.button {
		border-top: 2rpx solid rgba(0, 0, 0, 0.1);
		line-height: 40px;
		height: 80rpx;
		text-align: center;
		font-size: 14px;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #1E6FFF;
	}

	.line {
		width: 100%;
		background: rgba(0, 0, 0, 0.1);
		transition: height .3s;
	}

	.collapse {
		width: 100%;
		background: #FFFFFF;
		box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.1000);
		// border-radius: 10px;
		margin-bottom: 20rpx;

		.collapse_title {
			// padding: 20rpx;
			padding: 16rpx 32rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.collapse_name {
				font-size: 14px;
				// font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #171717;
				line-height: 20px;
			}

			image {
				width: 16px;
				height: 16px;
				transition: transform .3s, -webkit-transform .3s;
			}
		}

		.has-icon {
			position: relative;

			.collapse_name {
				margin-left: 12rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #333333;
			}
		}

		.has-icon::after {
			position: absolute;
			content: "";
			width: 6rpx;
			height: 29rpx;
			background: #1E6FFF;
		}

		.collapse_action {
			transition: height .4s ease-in-out, -webkit-transform .4s ease-in-out, transform .4s ease-in-out;
			transform-origin: 50% 50% 0px;
			overflow: hidden;
		}
	}
</style>